<?php
/**
 * Cobalt by MintJoomla
 * a component for Joomla! 1.7 - 2.5 CMS (http://www.joomla.org)
 * Author Website: http://www.mintjoomla.com/
 * @copyright Copyright (C) 2012 MintJoomla (http://www.mintjoomla.com). All rights reserved.
 * @license GNU/GPL http://www.gnu.org/copyleft/gpl.html
 */
defined('_JEXEC') or die('Restricted access');
$started = false;
$params = $this->tmpl_params;
$bEditMode = $this->item->id > 0;
if ($params->get('tmpl_params.form_grouping_type', 0)) {
    $started = true;
}
$k = 0;

$apix_config = JComponentHelper::getComponent('com_emails');
$apix_configui_url = $apix_config->params->get("configui_url");

if($apix_configui_url == '' || $apix_configui_url == 'http://') {

    JFactory::getApplication()->enqueueMessage(sprintf(JText::_('CONFIGUI_NOT_CORRECTLY_CONFIGURED'), JUri::root() . 'administrator/index.php?option=com_config&view=component&component=com_emails'));

} else {

    if(substr($apix_configui_url, -1) != '/') {
        $apix_configui_url .= '/';
    }
?>

    <link type="text/css" rel="stylesheet" href="<?php echo $apix_configui_url . 'ConfigUI'; ?>/mapper/themes/default/style.min.css" />
    <link type="text/css" rel="stylesheet" href="<?php echo $apix_configui_url . 'ConfigUI'; ?>/mapper/themes/default/jquery-ui.css" />
    <link type="text/css" rel="stylesheet" href="<?php echo $apix_configui_url . 'ConfigUI'; ?>/mapper/themes/default/layout-default-latest.css" />
    <link type="text/css" rel="stylesheet" href="<?php echo $apix_configui_url . 'ConfigUI'; ?>/mapper/themes/default/jquery.contextMenu.css" />
    <link type="text/css" rel="stylesheet" href="<?php echo $apix_configui_url . 'ConfigUI'; ?>/mapper/themes/default/visualmapper.min.css" />
    <link type="text/css" rel="stylesheet" href="<?php echo $apix_configui_url . 'ConfigUI'; ?>/mapper/themes/customTheme.css" />

    <script type="text/javascript" src="<?php echo $apix_configui_url . 'ConfigUI'; ?>/mapper/lib/jquery-ui.min.js"></script>
    <script type="text/javascript" src="<?php echo $apix_configui_url . 'ConfigUI'; ?>/mapper/lib/jstree.min.js"></script>
    <script type="text/javascript" src="<?php echo $apix_configui_url . 'ConfigUI'; ?>/mapper/lib/jquery.jsPlumb-1.4.1-all-min.js"></script>
    <script type="text/javascript" src="<?php echo $apix_configui_url . 'ConfigUI'; ?>/mapper/lib/jquery.layout-latest.js"></script>
    <script type="text/javascript" src="<?php echo $apix_configui_url . 'ConfigUI'; ?>/mapper/lib/jquery.contextMenu.js"></script>
    <script type="text/javascript" src="<?php echo $apix_configui_url . 'ConfigUI'; ?>/mapper/visualmapper.min.js"></script>

<?php } ?>
    <script type="text/javascript">
        if(typeof VisualMapper !== 'function') {

            jQuery(function() {

                Joomla.showError([
                    '<?php echo JText::_('VISUAL_MAPPER_NOT_LOADED'); ?>'
                ]);
            });
        }
    </script>
    <style>
        .licon {
            float: right;
            margin-left: 5px;
        }

        .line-brk {
            margin-left: 0px !important;
        }

        .editor textarea {
            box-sizing: border-box;
        }

        .control-group {
            margin-bottom: 10px;
            padding: 8px 0;
            -webkit-transition: all 200ms ease-in-out;
            -moz-transition: all 200ms ease-in-out;
            -o-transition: all 200ms ease-in-out;
            -ms-transition: all 200ms ease-in-out;
            transition: all 200ms ease-in-out;
        }

        .highlight-element {
            -webkit-animation-name: glow;
            -webkit-animation-duration: 1.5s;
            -webkit-animation-iteration-count: 1;
            -webkit-animation-direction: alternate;
            -webkit-animation-timing-function: ease-out;

            -moz-animation-name: glow;
            -moz-animation-duration: 1.5s;
            -moz-animation-iteration-count: 1;
            -moz-animation-direction: alternate;
            -moz-animation-timing-function: ease-out;

            -ms-animation-name: glow;
            -ms-animation-duration: 1.5s;
            -ms-animation-iteration-count: 1;
            -ms-animation-direction: alternate;
            -ms-animation-timing-function: ease-out;
        }

        <?php echo $params->get('tmpl_params.css');?>
        @-webkit-keyframes glow {
            0% {
                background-color: #fdd466;
            }
            100% {
                background-color: transparent;
            }
        }

        @-moz-keyframes glow {
            0% {
                background-color: #fdd466;
            }
            100% {
                background-color: transparent;
            }
        }

        @-ms-keyframes glow {
            0% {
                background-color: #fdd466;
            }
            100% {
                background-color: transparent;
            }
        }

    </style>

    <div class="form-horizontal">
<?php if (in_array($params->get('tmpl_params.form_grouping_type', 0), array(1, 4))): ?>
    <div class="tabbable<?php if ($params->get('tmpl_params.form_grouping_type', 0) == 4) echo ' tabs-left' ?>">
    <ul class="nav nav-tabs" id="tabs-list">
        <li><a href="#tab-main"
               data-toggle="tab"><?php echo JText::_($params->get('tmpl_params.tab_main', 'Main')); ?></a></li>

        <?php if (isset($this->sorted_fields)): ?>
            <?php foreach ($this->sorted_fields as $group_id => $fields) : ?>
                <?php if ($group_id == 0) continue; ?>
                <li><a class="taberlink" href="#tab-<?php echo $group_id ?>"
                       data-toggle="tab"><?php echo HTMLFormatHelper::icon($this->field_groups[$group_id]['icon']) ?> <?php echo $this->field_groups[$group_id]['name'] ?></a>
                </li>
            <?php endforeach; ?>
        <?php endif; ?>

        <?php if (count($this->meta)): ?>
            <li><a href="#tab-meta" data-toggle="tab"><?php echo JText::_('Meta Data'); ?></a></li>
        <?php endif; ?>
        <?php if (count($this->core_admin_fields)): ?>
            <li><a href="#tab-special" data-toggle="tab"><?php echo JText::_('Special Fields'); ?></a></li>
        <?php endif; ?>
        <?php if (count($this->core_fields)): ?>
            <li><a href="#tab-core" data-toggle="tab"><?php echo JText::_('Core Fields'); ?></a></li>
        <?php endif; ?>
    </ul>
<?php endif; ?>
<?php group_start($this, $params->get('tmpl_params.tab_main', 'Main'), 'tab-main'); ?>

<?php if ($params->get('tmpl_params.tab_main_descr')): ?>
    <?php echo $params->get('tmpl_params.tab_main_descr'); ?>
<?php endif; ?>

<?php if ($this->type->params->get('properties.item_title', 1) == 1): ?>
    <div class="control-group odd<?php echo $k = 1 - $k ?>">
        <label id="title-lbl" for="jform_title" class="control-label">
            <?php if ($params->get('tmpl_core.form_title_icon', 1)): ?>
                <?php echo HTMLFormatHelper::icon($params->get('tmpl_core.item_icon_title_icon', 'edit.png')); ?>
            <?php endif; ?>

            <?php echo JText::_($this->tmpl_params->get('tmpl_core.form_label_title', 'Title')) ?>
            <span class="pull-right" rel="tooltip" data-original-title="<?php echo JText::_('CREQUIRED') ?>">
					<?php echo HTMLFormatHelper::icon('asterisk-small.png'); ?></span>
        </label>

        <div class="controls">
            <div id="field-alert-title" class="alert alert-error" style="display:none"></div>
            <div class="row-fluid">
                <?php echo $this->form->getInput('title'); ?>
            </div>
        </div>
    </div>
<?php else : ?>
    <input type="hidden" name="jform[title]"
           value="<?php echo htmlentities(!empty($this->item->title) ? $this->item->title : JText::_('CNOTITLE') . ': ' . time(), ENT_COMPAT, 'UTF-8') ?>"/>
<?php endif; ?>

<?php if ($this->anywhere) : ?>
    <div class="control-group odd<?php echo $k = 1 - $k ?>">
        <label id="anywhere-lbl" class="control-label">
            <?php if ($params->get('tmpl_core.form_anywhere_icon', 1)): ?>
                <?php echo HTMLFormatHelper::icon('document-share.png'); ?>
            <?php endif; ?>

            <?php echo JText::_($this->tmpl_params->get('tmpl_core.form_label_anywhere', 'Where to post')) ?>
            <span class="pull-right" rel="tooltip"
                  data-original-title="<?php echo JText::_('CREQUIRED') ?>"><?php echo HTMLFormatHelper::icon('asterisk-small.png'); ?></span>
        </label>

        <div class="controls">
            <div id="field-alert-anywhere" class="alert alert-error" style="display:none"></div>
            <?php echo JHTML::_('cusers.wheretopost', @$this->item); ?>
        </div>
    </div>


    <div class="control-group odd<?php echo $k = 1 - $k ?>">
        <label id="anywherewho-lbl" for="whorepost" class="control-label">
            <?php if ($params->get('tmpl_core.form_anywhere_who_icon', 1)): ?>
                <?php echo HTMLFormatHelper::icon('arrow-retweet.png'); ?>
            <?php endif; ?>

            <?php echo JText::_($this->tmpl_params->get('tmpl_core.form_label_anywhere_who', 'Who can repost')) ?>
        </label>

        <div class="controls">
            <div id="field-alert-anywhere" class="alert alert-error" style="display:none"></div>
            <?php echo $this->form->getInput('whorepost'); ?>
        </div>
    </div>
<?php endif; ?>

<?php if (in_array($this->params->get('submission.allow_category'), $this->user->getAuthorisedViewLevels()) && $this->section->categories): ?>
    <div class="control-group odd<?php echo $k = 1 - $k ?>">
        <?php if ($this->catsel_params->get('tmpl_core.category_label', 0)): ?>
            <label id="category-lbl" for="category" class="control-label">
                <?php if ($params->get('tmpl_core.form_category_icon', 1)): ?>
                    <?php echo HTMLFormatHelper::icon('category.png'); ?>
                <?php endif; ?>

                <?php echo JText::_($this->tmpl_params->get('tmpl_core.form_label_category', 'Category')) ?>

                <?php if (!$this->type->params->get('submission.first_category', 0) && in_array($this->type->params->get('submission.allow_category', 1), $this->user->getAuthorisedViewLevels())) : ?>
                    <span class="pull-right" rel="tooltip"
                          data-original-title="<?php echo JText::_('CREQUIRED') ?>"><?php echo HTMLFormatHelper::icon('asterisk-small.png'); ?></span>
                <?php endif; ?>
            </label>
        <?php endif; ?>
        <div class="controls">
            <div id="field-alert-category" class="alert alert-error" style="display:none"></div>
            <?php if (!empty($this->allow_multi_msg)): ?>
                <div class="alert alert-warning">
                    <?php echo JText::_($this->type->params->get('emerald.type_multicat_subscription_msg')); ?>
                    <a href="<?php echo EmeraldApi::getLink('list', TRUE, $this->type->params->get('emerald.type_multicat_subscription')); ?>"><?php echo JText::_('CSUBSCRIBENOW'); ?></a>
                </div>
            <?php endif; ?>
            <?php echo $this->loadTemplate('category_' . $params->get('tmpl_params.tmpl_category', 'default')); ?>
        </div>
    </div>
<?php elseif (!empty($this->category->id)): ?>
    <div class="control-group odd<?php echo $k = 1 - $k ?>">
        <label id="category-lbl" for="category" class="control-label">
            <?php if ($params->get('tmpl_core.form_category_icon', 1)): ?>
                <?php echo HTMLFormatHelper::icon('category.png'); ?>
            <?php endif; ?>

            <?php echo JText::_($this->tmpl_params->get('tmpl_core.form_label_category', 'Category')) ?>

            <?php if (!$this->type->params->get('submission.first_category', 0) && in_array($this->type->params->get('submission.allow_category', 1), $this->user->getAuthorisedViewLevels())) : ?>
                <span class="pull-right" rel="tooltip"
                      data-original-title="<?php echo JText::_('CREQUIRED') ?>"><?php echo HTMLFormatHelper::icon('asterisk-small.png'); ?></span>
            <?php endif; ?>
        </label>

        <div class="controls">
            <div id="field-alert-category" class="alert alert-error" style="display:none"></div>
            <?php echo $this->section->name; ?>/<?php echo $this->category->path; ?>
        </div>
    </div>
<?php endif; ?>


<?php if ($this->ucategory) : ?>
    <div class="control-group odd<?php echo $k = 1 - $k ?>">
        <label id="ucategory-lbl" for="ucatid" class="control-label">
            <?php if ($params->get('tmpl_core.form_ucategory_icon', 1)): ?>
                <?php echo HTMLFormatHelper::icon('category.png'); ?>
            <?php endif; ?>

            <?php echo JText::_($this->tmpl_params->get('tmpl_core.form_label_ucategory', 'Category')) ?>

            <span class="pull-right" rel="tooltip"
                  data-original-title="<?php echo JText::_('CREQUIRED') ?>"><?php echo HTMLFormatHelper::icon('asterisk-small.png'); ?></span>
        </label>

        <div class="controls">
            <div id="field-alert-ucat" class="alert alert-error" style="display:none"></div>
            <?php echo $this->form->getInput('ucatid'); ?>
        </div>
    </div>
<?php else: ?>
    <?php $this->form->setFieldAttribute('ucatid', 'type', 'hidden'); ?>
    <?php $this->form->setValue('ucatid', null, '0'); ?>
    <?php echo $this->form->getInput('ucatid'); ?>
<?php endif; ?>

<?php if ($this->multirating): ?>
    <div class="control-group odd<?php echo $k = 1 - $k ?>">
        <label id="jform_multirating-lbl" class="control-label" for="jform_multirating">
            <?php echo strip_tags($this->form->getLabel('multirating')); ?>
            <span class="pull-right" rel="tooltip"
                  data-original-title="<?php echo JText::_('CREQUIRED') ?>"><?php echo HTMLFormatHelper::icon('asterisk-small.png'); ?></span>
        </label>

        <div class="controls">
            <div id="field-alert-rating" class="alert alert-error" style="display:none"></div>
            <?php echo $this->multirating; ?>
        </div>
    </div>
<?php endif; ?>


<?php if (isset($this->sorted_fields[0])): ?>
    <?php foreach ($this->sorted_fields[0] as $field_id => $field): ?>
        <div id="fld-<?php echo $field->id; ?>"
             class="control-group odd<?php echo $k = 1 - $k ?> <?php echo 'field-' . $field_id; ?> <?php echo $field->fieldclass; ?>">
            <?php if ($field->params->get('core.show_lable') == 1 || $field->params->get('core.show_lable') == 3): ?>
                <label id="lbl-<?php echo $field->id; ?>" for="field_<?php echo $field->id; ?>"
                       class="control-label <?php echo $field->class; ?>">
                    <?php if ($field->params->get('core.icon') && $params->get('tmpl_core.item_icon_fields')): ?>
                        <?php echo HTMLFormatHelper::icon($field->params->get('core.icon')); ?>
                    <?php endif; ?>


                    <?php if ($field->required): ?>
                        <span class="pull-right" rel="tooltip"
                              data-original-title="<?php echo JText::_('CREQUIRED') ?>"><?php echo HTMLFormatHelper::icon('asterisk-small.png'); ?></span>
                    <?php endif; ?>

                    <?php if ($field->description): ?>
                        <span class="pull-right" rel="tooltip" style="cursor: help;"
                              data-original-title="<?php echo htmlentities(($field->translateDescription ? JText::_($field->description) : $field->description), ENT_COMPAT, 'UTF-8'); ?>">
								<?php echo HTMLFormatHelper::icon('question-small-white.png'); ?>
							</span>
                    <?php endif; ?>

                    <?php echo $field->label; ?>

                </label>
                <?php if (in_array($field->params->get('core.label_break'), array(1, 3))): ?>
                    <div style="clear: both;"></div>
                <?php endif; ?>
            <?php endif; ?>

            <div
                class="controls<?php if (in_array($field->params->get('core.label_break'), array(1, 3))) echo '-full'; ?><?php echo(in_array($field->params->get('core.label_break'), array(1, 3)) ? ' line-brk' : NULL) ?><?php echo $field->fieldclass ?>">
                <div id="field-alert-<?php echo $field->id ?>" class="alert alert-error" style="display:none"></div>
                <?php echo $field->result; ?>
            </div>
        </div>
    <?php endforeach; ?>
    <?php unset($this->sorted_fields[0]); ?>
<?php endif; ?>

<?php if ((MECAccess::allowAccessAuthor($this->type, 'properties.item_can_add_tag', $this->item->user_id) || MECAccess::allowUserModerate($this->user, $this->section, 'allow_tags')) &&
    $this->type->params->get('properties.item_can_view_tag')
): ?>
    <div class="control-group odd<?php echo $k = 1 - $k ?>">
        <label id="tags-lbl" for="tags" class="control-label">
            <?php if ($params->get('tmpl_core.form_tags_icon', 1)): ?>
                <?php echo HTMLFormatHelper::icon('price-tag.png'); ?>
            <?php endif; ?>
            <?php echo JText::_($this->tmpl_params->get('tmpl_core.form_label_tags', 'Tags')) ?>
        </label>

        <div class="controls">
            <?php //echo JHtml::_('tags.tagform', $this->section, json_decode($this->item->tags, TRUE), array(), 'jform[tags]'); ?>
            <?php echo $this->form->getInput('tags'); ?>
        </div>
    </div>
<?php endif; ?>

<?php group_end($this); ?>


<?php if (isset($this->sorted_fields)): ?>
    <?php foreach ($this->sorted_fields as $group_id => $fields) : ?>
        <?php $started = true; ?>
        <?php group_start($this, $this->field_groups[$group_id]['name'], 'tab-' . $group_id); ?>
        <?php if (!empty($this->field_groups[$group_id]['descr'])): ?>
            <?php echo $this->field_groups[$group_id]['descr']; ?>
        <?php endif; ?>
        <?php foreach ($fields as $field_id => $field): ?>
            <div id="fld-<?php echo $field->id; ?>"
                 class="control-group odd<?php echo $k = 1 - $k ?> <?php echo 'field-' . $field_id; ?> <?php echo $field->fieldclass; ?>">
                <?php if ($field->params->get('core.show_lable') == 1 || $field->params->get('core.show_lable') == 3): ?>
                    <label id="lbl-<?php echo $field->id; ?>" for="field_<?php echo $field->id; ?>"
                           class="control-label <?php echo $field->class; ?>">
                        <?php if ($field->params->get('core.icon') && $params->get('tmpl_core.item_icon_fields')): ?>
                            <?php echo HTMLFormatHelper::icon($field->params->get('core.icon')); ?>
                        <?php endif; ?>
                        <?php if ($field->required): ?>
                            <span class="pull-right" rel="tooltip"
                                  data-original-title="<?php echo JText::_('CREQUIRED') ?>"><?php echo HTMLFormatHelper::icon('asterisk-small.png'); ?></span>
                        <?php endif; ?>

                        <?php if ($field->description): ?>
                            <span class="pull-right" rel="tooltip" style="cursor: help;"
                                  data-original-title="<?php echo htmlspecialchars(($field->translateDescription ? JText::_($field->description) : $field->description), ENT_COMPAT, 'UTF-8'); ?>">
									<?php echo HTMLFormatHelper::icon('question-small-white.png'); ?>
								</span>
                        <?php endif; ?>
                        <?php echo $field->label; ?>
                    </label>
                    <?php if (in_array($field->params->get('core.label_break'), array(1, 3))): ?>
                        <div style="clear: both;"></div>
                    <?php endif; ?>
                <?php endif; ?>

                <div
                    class="controls<?php if (in_array($field->params->get('core.label_break'), array(1, 3))) echo '-full'; ?><?php echo(in_array($field->params->get('core.label_break'), array(1, 3)) ? ' line-brk' : NULL) ?><?php echo $field->fieldclass ?>">
                    <div id="field-alert-<?php echo $field->id ?>" class="alert alert-error" style="display:none"></div>
                    <?php echo $field->result; ?>
                </div>
            </div>
        <?php endforeach; ?>
        <?php group_end($this); ?>
    <?php endforeach; ?>
<?php endif; ?>

<?php if (count($this->meta)): ?>
    <?php $started = true ?>
    <?php group_start($this, JText::_('CSEO'), 'tab-meta'); ?>
    <?php foreach ($this->meta as $label => $meta_name): ?>
        <div class="control-group odd<?php echo $k = 1 - $k ?>">
            <label id="jform_meta_descr-lbl" class="control-label" title="" for="jform_<?php echo $meta_name; ?>">
                <?php echo JText::_($label); ?>
            </label>

            <div class="controls">
                <div class="row-fluid">
                    <?php echo $this->form->getInput($meta_name); ?>
                </div>
            </div>
        </div>
    <?php endforeach; ?>

    <?php group_end($this); ?>
<?php endif; ?>



<?php if (count($this->core_admin_fields)): ?>
    <?php $started = true ?>
    <?php group_start($this, 'Special Fields', 'tab-special'); ?>
    <div class="admin">
        <?php foreach ($this->core_admin_fields as $key => $field): ?>
            <div class="control-group odd<?php echo $k = 1 - $k ?>">
                <label id="jform_<?php echo $field ?>-lbl" class="control-label"
                       for="jform_<?php echo $field ?>"><?php echo strip_tags($this->form->getLabel($field)); ?></label>

                <div class="controls field-<?php echo $field; ?>">
                    <?php echo $this->form->getInput($field); ?>
                </div>
            </div>
        <?php endforeach; ?>
    </div>
    <?php group_end($this); ?>
<?php endif; ?>
<?php if (count($this->core_fields)): ?>
    <?php group_start($this, 'Core Fields', 'tab-core'); ?>
    <?php foreach ($this->core_fields as $key => $field): ?>
        <div class="control-group odd<?php echo $k = 1 - $k ?>">
            <label id="jform_<?php echo $field ?>-lbl" class="control-label" for="jform_<?php echo $field ?>">
                <?php if ($params->get('tmpl_core.form_' . $field . '_icon', 1)): ?>
                    <?php echo HTMLFormatHelper::icon('core-' . $field . '.png'); ?>
                <?php endif; ?>
                <?php echo strip_tags($this->form->getLabel($field)); ?>
            </label>

            <div class="controls">
                <?php echo $this->form->getInput($field); ?>
            </div>
        </div>
    <?php endforeach; ?>
    <?php group_end($this); ?>
<?php endif; ?>

<?php if ($started): ?>
    <?php total_end($this); ?>
<?php endif; ?>
    <br/>
    </div>

    <script type="text/javascript">
        <?php if(in_array($params->get('tmpl_params.form_grouping_type', 0), array(1,4))):?>
        jQuery('#tabs-list a:first').tab('show');
        <?php elseif(in_array($params->get('tmpl_params.form_grouping_type', 0), array(2))):?>
        jQuery('#tab-main').collapse('show');
        <?php endif;?>
    </script>




<script>
        jQuery(function () {
            var origInputSchema = jQuery('input[name="orig_content_202"]'),
                origOutputSchema = jQuery('input[name="orig_content_203"]'),
                generateXSLTDisplay = jQuery('textarea[name="jform[fields][204]"]'),
                schemaMapper = jQuery('input[name="jform[fields][208]"]'),
                responseType = jQuery('select[name="jform[fields][212]"]'),
                mapper = jQuery('#mapper2'),

                showMapperBtn = jQuery('#show-mapper>button'),
                visualMapperXSLTGeneratorBtn = jQuery('.visualMapperXsltGenerator'),
                mapperBtn = jQuery('.mapperBtn');

                decodeXML();

                var originalName = jQuery('input[name="jform[title]"]').val(),
                origInputSchemaVal = origInputSchema.val(),
                origOutputSchemaVal = origOutputSchema.val(),
                origGenerateXSLTDisplayVal = generateXSLTDisplay.val(),
                origSchemaMapVal = schemaMapper.val(),
                origResponseTypeVal = responseType.val(),

                newInputSchemaVal = origInputSchema.val(),
                newOutputSchemaVal = origOutputSchema.val(),
                newGenerateXSLTDisplayVal = generateXSLTDisplay.val(),
                newSchemaMapVal = schemaMapper.val(),

                regStr = /[\r\n\t]+/g,

                <?php // this is intentionally using the Php echo function because the "<?" pair would be translated as Php code delimiters if they are not inside "<?php" block. ?>
                emptyXMLDocStr = '<?php echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?><metadata></metadata>"; ?>',

                schemaMapObj = {},
                errorArr = [];

            jQuery("textarea").height(80);

            hideEle(mapper);
            hideEle(mapperBtn);
            hideEle(generateXSLTDisplay);

            if(origInputSchemaVal !== "" && origOutputSchema){
                jQuery('#parent_list202>.alert').append("<input type='hidden' name='content_202' value='"+ origInputSchemaVal +"'>");
                jQuery('#parent_list203>.alert').append("<input type='hidden' name='content_203' value='"+ origInputSchemaVal +"'>");
            }
            if(origGenerateXSLTDisplayVal !== ""){
                showEle(generateXSLTDisplay);
            }

            jQuery("#modal202,#modal203").on('hide', function(e) {
                if(jQuery('[name="jform[fields][202][]"]').length!==0 && jQuery('[name="jform[fields][203][]"]').length!==0){
                    if(!showMapperBtn.hasClass('show-mapper')){
                        removeMapper();
                        jQuery('#show-mapper>.show-mapper').trigger('click');
                    }else{
                        jQuery('#show-mapper>.show-mapper').trigger('click');
                    }

                }
            });

            showMapperBtn.on('click', function () {
                jQuery("#parent_list202 a.close,#parent_list203 a.close").on('click', function(){
                    var currentId = jQuery(this).parent().parent().attr('id');
                    var parentListId202 = jQuery('#parent_list202').attr('id');
                    var parentListId203 = jQuery('#parent_list203').attr('id');
                    removeMapper();
                    if(currentId == parentListId202){
                        newInputSchemaVal = emptyXMLDocStr;
                    }else if(currentId == parentListId203){
                        newOutputSchemaVal = emptyXMLDocStr;
                    }
                    newGenerateXSLTDisplayVal = "";
                    collectSchemaMap();
                    newSchemaMapVal = schemaMapper.val();
                    showMapper();

                });

                if(jQuery(this).hasClass('show-mapper')){
                    newInputSchemaVal = jQuery('input[name="content_202"]').length == 0 ? emptyXMLDocStr : jQuery('input[name="content_202"]').val();
                    newOutputSchemaVal = jQuery('input[name="content_203"]').length == 0 ? emptyXMLDocStr : jQuery('input[name="content_203"]').val();
                    newGenerateXSLTDisplayVal = generateXSLTDisplay.val();
                    collectSchemaMap();
                    newSchemaMapVal = schemaMapper.val();
                    showMapper();
                }else{
                    removeMapper();
                }
            });

            visualMapperXSLTGeneratorBtn.on('click', function () {
                showEle(generateXSLTDisplay);
                generateXSLT('#mapper2');
            });


            jQuery(".removeSchemaMap").on('click', function () {
                jQuery(this).parent().remove();
            });

            /*
             remove mapper
             */
            function removeMapper(){
                showMapperBtn.text("Show");
                showMapperBtn.removeClass("mapper-remove");
                showMapperBtn.addClass("show-mapper");

                destroyMapper('#mapper2');
                hideEle(mapper);
                hideEle(mapperBtn);
                hideEle(generateXSLTDisplay);
                generateXSLTDisplay[0].value = '';
            }

            //define remove "disabled" attribute
            function removeDisabled(ele){
                ele.removeClass("disabled");
                ele.removeAttr("disabled");
            }

            //define add "disabled" attribute
            function addDisabled(ele){
                ele.addClass('disabled');
                ele.attr("disabled", true);
            }


            /**
             * Generate XSLT based on the provided mapper
             * @param mapper
             */
            function generateXSLT(mapper) {
                if(jQuery(mapper).visualMapper && typeof(jQuery(mapper).visualMapper == "function")){
                    generateXSLTDisplay[0].value = jQuery(mapper).visualMapper('generateXSLT');
            }
            }

            /**
             * Destroy the given mapper
             */
            function destroyMapper(mapper) {
                if(jQuery(mapper).visualMapper && typeof(jQuery(mapper).visualMapper == "function")) {
                    jQuery(mapper).visualMapper('destroy');
                }
            }

            /**
             *  schema map collection
             */
            function collectSchemaMap(){
                schemaMapObj = {};
                jQuery.each(jQuery('.schema-map-body'), function () {
                    var key = jQuery(this).find('input[name="uri"]').val();
                    var value = jQuery.trim(jQuery(this).find('textarea[name="schemaMapXML"]').val()).replace(regStr, ' ');
                    schemaMapObj[key] = value;
                });
                schemaMapper.val(JSON.stringify(schemaMapObj));
            }

            //show mapper
            function showMapper(){
                // Define Function provider
                var functionProvider = function (tabName) {
                    var functionsTabs = {};
                    functionsTabs["data"] = '<Tab><name>Data</name><type>Data</type><valueSeparator>/</valueSeparator><content></content></Tab>';
                    functionsTabs["function"] = '<Tab><name>Function</name><type>Function</type><content><category><name>String</name><function><name>concat</name><noOfArguments>2</noOfArguments><dragString>concat(&lt;&lt;string1&gt;&gt;, &lt;&lt;string2&gt;&gt;)</dragString><information>Returns the concatenation of the function parameters. You can concatenate two or more strings.</information><returnType>String</returnType></function><function><name>concat-sequence</name><noOfArguments>1</noOfArguments><dragString>concat-sequence(&lt;&lt;nodeset&gt;&gt;)</dragString><information>Concatenates the string value of each node in order.</information><returnType>String</returnType></function><function><name>lower-case</name><noOfArguments>1</noOfArguments><dragString>lower-case(&lt;&lt;string&gt;&gt;)</dragString><information>Returns a new string where all the upper-case characters have been converted to lower-case. All other characters are unchanged.</information><returnType>String</returnType></function><function><name>index-of</name><noOfArguments>2</noOfArguments><dragString>index-of(&lt;&lt;string&gt;&gt; , &lt;&lt;test-string&gt;&gt;)</dragString><information>Returns the 1-based index of the first occurrence of second string int first first string. Returns -1 if there is no match. </information><returnType>int</returnType></function><function><name>string-length</name><noOfArguments>1</noOfArguments><dragString>string-length(&lt;&lt;string&gt;&gt;)</dragString><information>Returns the number of characters in the string.</information><returnType>int</returnType></function><function><name>upper-case</name><noOfArguments>1</noOfArguments><dragString>upper-case(&lt;&lt;string&gt;&gt;)</dragString><information>Returns a new string where all the lower-case characters have been converted to upper-case. All other characters are unchanged.</information><returnType>String</returnType></function></category><category><name>Logical</name><function><name>not</name><noOfArguments>1</noOfArguments><dragString>not(&lt;&lt;boolean&gt;&gt;)</dragString><information>Returns true if the parameter is false, and false otherwise</information><returnType>boolean</returnType></function><function><name>(or)</name><noOfArguments>2</noOfArguments><dragString>&lt;&lt;boolean1&gt;&gt; or &lt;&lt;boolean2&gt;&gt;</dragString><information>Performs logical or on the two parameters.</information><returnType>boolean</returnType></function><function><name>(and)</name><noOfArguments>2</noOfArguments><dragString>&lt;&lt;boolean1&gt;&gt; and &lt;&lt;boolean2&gt;&gt;</dragString><information>Performs logical and on the two parameters.</information><returnType>boolean</returnType></function></category><category><name>Number</name><function><name>(Addition)</name><noOfArguments>2</noOfArguments><dragString>&lt;&lt;number1&gt;&gt; + &lt;&lt;number2&gt;&gt;</dragString><information>Adds two numbers</information><returnType>double</returnType></function><function><name>(Subtraction)</name><noOfArguments>2</noOfArguments><dragString>&lt;&lt;number1&gt;&gt; - &lt;&lt;number2&gt;&gt;</dragString><information>Subtracts 2 numbers.</information><returnType>double</returnType></function><function><name>(Multiplication)</name><noOfArguments>2</noOfArguments><dragString>&lt;&lt;number1&gt;&gt; * &lt;&lt;number2&gt;&gt;</dragString><information>Multiply two numbers</information><returnType>double</returnType></function><function><name>(Division)</name><noOfArguments>2</noOfArguments><dragString>&lt;&lt;number1&gt;&gt; / &lt;&lt;number2&gt;&gt;</dragString><information>Floating Point Division.</information><returnType>double</returnType></function><function><name>(Mod)</name><noOfArguments>2</noOfArguments><dragString>&lt;&lt;number1&gt;&gt; % &lt;&lt;number2&gt;&gt;</dragString><information>Returns the remainder from division</information><returnType>int</returnType></function></category><category><name>Conditions and Iteration</name><function><name>(if)</name><noOfArguments>3</noOfArguments><dragString>if (&lt;&lt;test&gt;&gt;) then &lt;&lt;then&gt;&gt; else &lt;&lt;else&gt;&gt;</dragString><information>Conditionally evaluates either the then-expression or the then-expression based on the boolean value of the test-expression</information><returnType>&lt;&lt;else&gt;&gt;</returnType></function><function><name>(for)</name><noOfArguments>3</noOfArguments><dragString>for $var in &lt;&lt;set&gt;&gt; return &lt;&lt;value&gt;&gt;</dragString><information>Itearates over a set, building a new set item-by-item.</information><returnType>&lt;&lt;value&gt;&gt;</returnType></function></category></content></Tab>';
                    functionsTabs["constant"] = '<Tab><name>Constant</name><type>Constant</type><content><category><name>General</name><constant><name>Ampersand</name><dragString>&amp;</dragString><information>Because & is the escape character, &amp; must be used to generate an ampersand.</information></constant></category><category><name>Whitespace</name><constant><name>Line Feed</name><dragString>&lf</dragString><information>&lf, the unicode character #10.</information></constant><constant><name>Tab</name><dragString> &tab</dragString><information>&tab, the unicode character #8.</information></constant><constant><name>Non-breaking Space</name><dragString>&nbps</dragString><information>&nps, the unicode character #160.</information></function></category><category><name>Quotes</name><constant><name>Single Quote</name><dragString> \'</dragString><information>Enclose single quotes in a double-quote string.</information></constant><constant><name>Double Quote</name><dragString>\"</dragString><information>Enclose double quotes in a single-quote string.</information></constant></category></content></Tab>';
                    functionsTabs["customfunctions"] = '<Tab><name>CustomFunctions</name><type>CustomFunctions</type><content><category><name>com</name><category><name>tibco</name><category><name>CustomFunctions1</name><function><name>getData1</name><noOfArguments>0</noOfArguments><dragString>java:com.tibco.CustomFunctions1:getData1()</dragString></function><function><name>getData2</name><noOfArguments>0</noOfArguments><dragString>java:com.tibco.CustomFunctions1:getData2()</dragString></function></category><category><name>CustomFunctions2</name><function><name>getValue1</name><noOfArguments>0</noOfArguments><dragString>java:com.tibco.CustomFunctions1:getValue1()</dragString></function><function><name>getValue2</name><noOfArguments>0</noOfArguments><dragString>java:com.tibco.CustomFunctions1:getValue2()</dragString></function></category></category></category></content></Tab>';
                    return functionsTabs[tabName];
                };
                try {
                    mapper.visualMapper({
                        inputTrees: [newInputSchemaVal || emptyXMLDocStr],
                        outputTrees: [newOutputSchemaVal || emptyXMLDocStr],
                        xslt: newGenerateXSLTDisplayVal,
                        left: "0",
                        top: "0",
                        height: "450",
                        width: "680px",
                        styleBasePath: "mapper",
                        schemaProvider: schemaMapObj,
                        functionProvider: functionProvider
                    });
                    mapper.visualMapper("displayMapper");

                    if (jQuery('input[name="content_202"]').val()!= '' && jQuery('input[name="content_203"]').val()!= '') {
                        showEle(mapperBtn);
                        mapper.show();

                        showMapperBtn.text("Close");
                        showMapperBtn.removeClass("show-mapper");
                        showMapperBtn.addClass("mapper-remove");
                    }

                    errorArr = [];
                } catch (e) {
                    Joomla.showError([e.message]);
                    errorArr.push(e.message);
                }

            }

            //define show element
            function showEle(ele){
                ele.show();
            }
            //define hide element
            function hideEle(ele){
                ele.hide();
            }


            function encodeXML() {
                jQuery('input[name="content_202"]').val(DeveloperPortal.java7EncodeURIComponent(jQuery('input[name="content_202"]').val()));
                jQuery('input[name="content_203"]').val(DeveloperPortal.java7EncodeURIComponent(jQuery('input[name="content_203"]').val()));
                generateXSLTDisplay.val(DeveloperPortal.java7EncodeURIComponent(generateXSLTDisplay.val()));
                schemaMapper.val(DeveloperPortal.java7EncodeURIComponent(schemaMapper.val()));
            }

            function decodeXML() {
                origInputSchema.val(DeveloperPortal.java7DecodeURIComponent(origInputSchema.val()));
                origOutputSchema.val(DeveloperPortal.java7DecodeURIComponent(origOutputSchema.val()));
                generateXSLTDisplay.val(DeveloperPortal.java7DecodeURIComponent(generateXSLTDisplay.val()));
                schemaMapper.val(DeveloperPortal.java7DecodeURIComponent(schemaMapper.val()));
            }


            Joomla.beforesubmitform = function (fCallback, fErrorback) {
                var currentName = jQuery('input[name="jform[title]"]').val(),
                    currentInputSchemaVal = jQuery('input[name="content_202"]').val(),
                    currentOutputSchemaVal = jQuery('input[name="content_203"]').val(),
                    currentGenerateXSLT = generateXSLTDisplay.val(),
                    currentSchemaMapVal = schemaMapper.val(),
                    currentResponseTypeVal = responseType.val();

                if (currentName !== originalName) {
                    window.oUpdatedFields = window.oUpdatedFields || {};
                    window.oUpdatedFields['name'] = originalName;
                }

                if (origInputSchemaVal !== currentInputSchemaVal) {
                    window.oUpdatedFields = window.oUpdatedFields || {};
                    window.oUpdatedFields[202] = [origInputSchemaVal];
                }

                if (origOutputSchemaVal !== currentOutputSchemaVal) {
                    window.oUpdatedFields = window.oUpdatedFields || {};
                    window.oUpdatedFields[203] = [origOutputSchemaVal];
                }

                if (origGenerateXSLTDisplayVal !== currentGenerateXSLT) {
                    window.oUpdatedFields = window.oUpdatedFields || {};
                    window.oUpdatedFields[204] = [origGenerateXSLTDisplayVal];
                }

                if (origSchemaMapVal !== currentSchemaMapVal) {
                    window.oUpdatedFields = window.oUpdatedFields || {};
                    window.oUpdatedFields[208] = [origSchemaMapVal];
                }

                if (origResponseTypeVal !== currentResponseTypeVal) {
                    window.oUpdatedFields = window.oUpdatedFields || {};
                    window.oUpdatedFields[212] = [origResponseTypeVal];
                }

                if(errorArr.length == 0){
                    encodeXML();
                    fCallback();
                }else{
                    jQuery('button.btn-submit').attr('disabled', false);
                }
            };

            <?php
                if($bEditMode){
                    echo "showMapperBtn.trigger('click');";
                }
            ?>

            });

    </script>




<?php
function group_start($data, $label, $name)
{
    static $start = false;
    switch ($data->tmpl_params->get('tmpl_params.form_grouping_type', 0)) {
        //tab
        case 4:
        case 1:
            if (!$start) {
                echo '<div class="tab-content" id="tabs-box">';
                $start = TRUE;
            }
            echo '<div class="tab-pane" id="' . $name . '">';
            break;
        //slider
        case 2:
            if (!$start) {
                echo '<div class="accordion" id="accordion2">';
                $start = TRUE;
            }
            echo '<div class="accordion-group">
				<div class="accordion-heading">
					<a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion2" href="#' . $name . '">
					     ' . $label . '
					</a>
				</div>
				<div id="' . $name . '" class="accordion-body collapse">
					<div class="accordion-inner">';
            break;
        // fieldset
        case 3:
            if ($name != 'tab-main') {
                echo "<legend>{$label}</legend>";
            }
            break;
    }
}

function group_end($data)
{
    switch ($data->tmpl_params->get('tmpl_params.form_grouping_type', 0)) {
        case 4:
        case 1:
            echo '</div>';
            break;
        case 2:
            echo '</div></div></div>';
            break;
    }
}

function total_end($data)
{
    switch ($data->tmpl_params->get('tmpl_params.form_grouping_type', 0)) {
        //tab
        case 4:
        case 1:
            echo '</div></div>';
            break;
        case 2:
            echo '</div>';
            break;
    }
}
